const router = require('koa-router')() // 一边导入一边实例化
// 操作数据库的接口
const { getToken, Queryurl } = require('../../config/databaseapi.js')
// 注册接口
router.post('/queryallcomment', async (ctx) => {
    console.log('调用查询一级评论接口')
    // const { queryParams } = ctx.request.body;
    console.log('查询参数',ctx.request.body);
    const queryParams = {};
    const total = await new getToken().posteve(Queryurl,`db.collection("allcomment").where(${JSON.stringify(queryParams)}).get()`);
    const page = Math.ceil(total.pager.Total);
    let tasks = [];
    for (let i=0;i<page;i++){
        const resPromise=new getToken().posteve(Queryurl,`db.collection("allcomment").where(${JSON.stringify(queryParams)}).skip(${i*10}).limit(10).get()`);
        tasks.push(resPromise);
    }
    const results = await Promise.all(tasks);
    let alldata = [];
    for (let i=0;i<results.length;i++){
        if(results[i].errcode===0){
            for (let j=0;j<results[i].data.length;j++){
                alldata.push(results[i].data[j]);
            }
        }
    }
    console.log('查询结果',alldata);
    // console.log(res);
    ctx.body = alldata;
})

module.exports = router.routes()